<?php
/**
 * @version		$Id: k2mart.php 25 2010-09-08 15:16:11Z joomlaworks $
 * @package		K2mart
 * @author    	JoomlaWorks http://www.joomlaworks.gr
 * @copyright	Copyright (c) 2006 - 2010 JoomlaWorks, a business unit of Nuevvo Webware Ltd. All rights reserved.
 * @license		Commercial
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.plugin.plugin');

class plgSystemK2mart extends JPlugin {

	function plgSystemK2mart(&$subject, $config) {
		parent::__construct($subject, $config);
	}

	function onAfterInitialise(){
		$mainframe = &JFactory::getApplication();
		jimport('joomla.filesystem.file');
		if($mainframe->isAdmin()){
			return;
		}
		if(!JFile::exists(JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'k2.php')){
			return;
		}
		if(!JFile::exists(JPATH_SITE.DS.'components'.DS.'com_virtuemart'.DS.'virtuemart.php')){
			return;
		}
		if(JRequest::getCmd('option')!='com_virtuemart'){
			return;
		}
		$params = &JComponentHelper::getParams('com_k2mart');
		$page = JRequest::getCmd('page');
		if($page=='shop.product_details'){
			$productID = JRequest::getInt('product_id');
			if($productID){
				$db = &JFactory::getDBO();
				$query = "SELECT itemID FROM #__k2mart WHERE productID={$productID}";
				$db->setQuery($query, 0, 1);
				$itemID = $db->loadResult();
				if(is_null($itemID)){
					return;
				}
				$query = "SELECT id, alias, catid FROM #__k2_items WHERE id={$itemID}";
				$db->setQuery($query, 0, 1);
				$item = $db->loadObject();
				if(is_null($item)){
					return;
				}
				$query = "SELECT alias FROM #__k2_categories WHERE id = {$item->catid}";
				$db->setQuery($query, 0, 1);
				$item->categoryAlias = $db->loadResult();
				JLoader::register('K2HelperRoute', JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'helpers'.DS.'route.php');
				$link = K2HelperRoute::getItemRoute($item->id.':'.urlencode($item->alias),$item->catid.':'.urlencode($item->categoryAlias));
				$mainframe->redirect(JRoute::_($link, false));
			}
		}
		if( ($page=='shop.browse' || $page=='shop.index' || $page=='') && $params->get('catalogRoot')){
			$catid = $params->get('catalogRoot');
			JLoader::register('K2HelperRoute', JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'helpers'.DS.'route.php');
			$link = K2HelperRoute::getCategoryRoute($catid);
			$mainframe->redirect(JRoute::_($link, false));
		}
		return;
	}

	function onAfterRoute(){
		$mainframe = &JFactory::getApplication();
		jimport('joomla.filesystem.file');
		if($mainframe->isSite()){
			return;
		}
		if(!JFile::exists(JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'k2.php')){
			return;
		}
		if(!JFile::exists(JPATH_SITE.DS.'components'.DS.'com_virtuemart'.DS.'virtuemart.php')){
			return;
		}
		if(JRequest::getCmd('option')=='com_k2' && JRequest::getCmd('view')=='items' && JRequest::getCmd('task')=='remove') {
			$IDs = JRequest::getVar('cid', array());
			JArrayHelper::toInteger($IDs);
			if(count($IDs)){
				$db = &JFactory::getDBO();
				$query = "DELETE FROM #__k2mart WHERE itemID IN (".implode(',', $IDs).")";
				$db->setQuery($query);
				$db->query();
			}
		}
		return;
	}
}
